<?php
include_once 'config.php';

define('FILE_NAME', "token.txt");

class File_Manager {
	static private $fileStream = NULL;
	
	function __construct(){
		self::$fileStream = fopen(BASE_PATH.FILE_NAME, 'a+');
		chmod(BASE_PATH.FILE_NAME, 0777);
	}
	
	/**
	 * Funcion Singleton de la clase
	 * @return File_Manager
	 */
	public static function getInstance(){
		if (self::$fileStream == NULL) {
			return new File_Manager();
		}
		return self::$fileStream;
	}
	
	/**
	 * Escribe el texto proporcionado al fichero.
	 * @param $token
	 */
	function setToken($token) {
		fwrite(self::$fileStream, $token);
		fclose(self::$fileStream);
	}
	
	/**
	 * Obtiene la cadena del token guardado
	 * @return Stings
	 */
	function getToken() {
		return file_get_contents(BASE_PATH.FILE_NAME);
	}
	
	/**
	 * Elimina el fichero
	 */
	function deleteFile() {
		unlink(BASE_PATH.FILE_NAME);
	}
	
	/**
	 * Funcion estática que determina si el fichero existe
	 */
	public static function exist(){
		return file_exists(BASE_PATH.FILE_NAME);
	}
}
